package nf.dao;

import nf.entity.CellPhone;
import nf.entity.User;
import nf.util.CloseHelpr;
import nf.util.ConnHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.ArrayList;

public class CellPhoneDaoImpl implements CellPhoneDao {
    PreparedStatement psmt = null;
    ResultSet rs = null;
    Connection conn = ConnHelper.getConnection();
    @Override
    public boolean add(CellPhone phone) {
        try {
            psmt = conn.prepareStatement("insert into cellphone(no,name,price) value(?,?,?)");
            psmt.setString(1,phone.getNo());
            psmt.setString(2,phone.getName());
            psmt.setFloat(3,phone.getPrice());
            psmt.executeUpdate();
            return true;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
           CloseHelpr.ClosePC(psmt,conn);
        }
        return false;
    }

    @Override
    public boolean deleteById(int id) {
        try {
            psmt = conn.prepareStatement("delete from cellphone where id=?");
            psmt.setInt(1,id);
            psmt.executeUpdate();
            return true;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            CloseHelpr.ClosePC(psmt,conn);
        }
        return false;
    }

    @Override
    public boolean updateById(CellPhone phone) {
        try {
            psmt = conn.prepareStatement("update cellphone set no=?,name=?,price=? where id=?");
            psmt.setString(1,phone.getNo());
            psmt.setString(2,phone.getName());
            psmt.setFloat(3,phone.getPrice());
            psmt.setInt(4,phone.getId());
            psmt.executeUpdate();
            return true;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            CloseHelpr.ClosePC(psmt,conn);
        }
            return false;
    }

    @Override
    public List<CellPhone> querByName(String name) {
        try {
            psmt = conn.prepareStatement("select * from cellphone where name like ?");
            psmt.setString(1,"%"+name+"%");
            rs=psmt.executeQuery();
            List<CellPhone> list = new ArrayList<CellPhone>();
            while (rs.next()){
                CellPhone phone = new CellPhone();
                phone.setId(rs.getInt("id"));
                phone.setName(rs.getString("name"));
                phone.setNo(rs.getString("no"));
                phone.setPrice(rs.getFloat("price"));
                list.add(phone);
            }
            return list;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
           CloseHelpr.CloseRPC(rs,psmt,conn);
        }
        return null;
    }

    @Override
    public List<CellPhone> quer() {
        List<CellPhone> phone = new ArrayList<CellPhone>();
        try {
            psmt = conn.prepareStatement("select * from cellphone");
            rs = psmt.executeQuery();
            while(rs.next()){
                CellPhone p = new CellPhone();
                p.setId(rs.getInt("id"));
                p.setNo(rs.getString("no"));
                p.setName(rs.getString("name"));
                p.setPrice(rs.getFloat("price"));
                phone.add(p);
            }
            return phone;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            CloseHelpr.CloseRPC(rs,psmt,conn);
        }
        return null;
    }

    public User select(User user){
        try {
            psmt = conn.prepareStatement("select * from user where userName=? and password=?");
            psmt.setString(1,user.getUserName());
            psmt.setString(2,user.getPassword());
            rs = psmt.executeQuery();
            if(rs.next()){
                User u = new User();
                u.setUserName(rs.getString("userName"));
                u.setPassword(rs.getString("password"));
                return u;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            CloseHelpr.CloseRPC(rs,psmt,conn);
        }
        return null;
    }

    public User selectByName(String name){
        try {
            psmt = conn.prepareStatement("select * from user where name=?");
            psmt.setString(1,name);
            rs = psmt.executeQuery();
            if(rs.next()){
                User u = new User();
                u.setUserName(rs.getString("userName"));
                u.setPassword(rs.getString("password"));
                return u;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            CloseHelpr.CloseRPC(rs,psmt,conn);
        }
        return null;
    }
    public User selectByUsname(String name){
        try {
            psmt = conn.prepareStatement("select * from user where username=?");
            psmt.setString(1,name);
            rs = psmt.executeQuery();
            if(rs.next()){
                User u = new User();
                u.setUserName(rs.getString("userName"));
                u.setPassword(rs.getString("password"));
                u.setName(rs.getString("name"));
                return u;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            CloseHelpr.CloseRPC(rs,psmt,conn);
        }
        return null;
    }

    public boolean updateUser(User user){
        try {
            psmt = conn.prepareStatement("update User set password=? where name=?");
            psmt.setString(1,user.getPassword());
            psmt.setString(2,user.getName());
            psmt.executeUpdate();
            return true;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
           CloseHelpr.ClosePC(psmt,conn);
        }
        return false;
    }

    public boolean adduser(User user){
        try {
            psmt = conn.prepareStatement("insert into user(userName,password,name) values(?,?,?)");
            psmt.setString(1,user.getUserName());
            psmt.setString(2,user.getPassword());
            psmt.setString(3,user.getName());
            psmt.executeUpdate();
            return true;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            CloseHelpr.ClosePC(psmt,conn);
        }
        return false;
    }
}
